package com.swpu.service_sport.controller;


import cn.hutool.core.date.DateUtil;
import com.swpu.commonutils.JwtUtils;
import com.swpu.commonutils.R;
import com.swpu.commonutils.ResultEncryption;
import com.swpu.service_sport.entity.SportRecord;
import com.swpu.service_sport.entity.vo.SportRecordVo;
import com.swpu.service_sport.service.SportRecordService;
import com.swpu.service_user.aop.Decrypt;
import com.swpu.service_user.aop.Encrypt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.DELETE;
import javax.xml.crypto.Data;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author chaichai
 * @since 2024-02-23
 */
@RestController
@RequestMapping("/service_sport/sport-record")
public class SportRecordController {
    @Autowired
    private SportRecordService sportRecordService;
    @PostMapping("/saveUserSportRecord")
    @Decrypt
    @Encrypt
    public R saveUserSportRecord(@RequestBody ResultEncryption resultEncryption,   SportRecord sportRecord, HttpServletRequest request){
        String openid = JwtUtils.getOpenidByJwtToken(request);
        if (openid.isEmpty()){
            return R.error().message("当前用户无访问权限");
        }
        sportRecordService.saveUserSportRecord(openid,sportRecord);
        return R.ok();
    }
    @GetMapping("/getUserSportRecord")
    public R getUserSportRecord(HttpServletRequest request, @RequestParam String date){
        String openid = JwtUtils.getOpenidByJwtToken(request);
        if (openid.isEmpty()){
            return R.error().message("当前用户无访问权限");
        }
//        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        try {
//            queryDate = format.parse(date);
//        } catch (ParseException e) {
//            throw new RuntimeException();
//        }
        Date queryDate= DateUtil.parseDate(date);
        List<SportRecordVo> sportRecord = sportRecordService.getUserSportRecord(openid, queryDate);
        return R.ok().data("sportRecord",sportRecord);
    }
    @GetMapping("/getSportCaloriesByDate")
    public  R getSportCaloriesByDate(HttpServletRequest request,@RequestParam String queryDate){
        String openid = JwtUtils.getOpenidByJwtToken(request);
        if (openid.isEmpty()){
            return R.error().message("当前用户无访问权限");
        }
        Integer sportCalories = sportRecordService.getSportCaloriesByDate(openid, DateUtil.parseDate(queryDate));
        return R.ok().data("sportCalories",sportCalories);
    }
    @GetMapping("getSportCountByDate")
    public R getSportCountByDate(HttpServletRequest request,@RequestParam String date){
        String openid = JwtUtils.getOpenidByJwtToken(request);
        if (openid.isEmpty()){
            return R.error().message("当前用户无访问权限");
        }
        Date queryDate=null;
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            queryDate = format.parse(date);
        } catch (ParseException e) {
            throw new RuntimeException();
        }
        int sportRecordCount = sportRecordService.getSportCountByDate(openid, queryDate);
        return R.ok().data("sportRecordCount",sportRecordCount);
    }
    @DeleteMapping("/deleteSportRecordById")
    public R deleteSportRecordById(HttpServletRequest request, @RequestParam String id){
        String openid = JwtUtils.getOpenidByJwtToken(request);
        if (openid.isEmpty()){
            return R.error().message("当前用户无访问权限");
        }
        sportRecordService.deleteSportRecordById(openid,id);
        return R.ok();
    }

}

